<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="开始时间:">
              <a-date-picker v-model:value="where.searchBeginTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss" valueFormat="YYYY-MM-DD HH:mm:ss" />
            </a-form-item>
            <a-form-item label="结束时间:">
              <a-date-picker v-model:value="where.searchEndTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss" valueFormat="YYYY-MM-DD HH:mm:ss" />
            </a-form-item>

            <a-form-item label='所属业务:' name='ownerBiz' style="width: 250px;">
              <a-select v-model:value="where.ownerBiz">
                <a-select-option value="herd-hr-rr">牛群-呼吸心跳数据源</a-select-option>
                <a-select-option value="herd-rumen-temp">牛群-瘤胃温度数据源</a-select-option>
                <a-select-option value="device-sprayctocabinet">设备-喷淋控制柜数据源</a-select-option>
                <a-select-option value="device-envcontrol">设备-环控设备数据源</a-select-option>
                <a-select-option value="fence-hr-rr">牛栏-呼吸心跳数据源</a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="objsAlertslogId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>

          <template #expandedRowRender="{ column, record }">
            <a-table :columns="subColumns" :data-source="record.monitorAlertsLogs" :pagination="false" />
          </template>

        </ele-pro-table>
      </a-card>
    </div>
  </div>
</template>

<script>
import {MonitorObjectsAlertsLogApi} from "@/api/monitors/MonitorObjectsAlertsLogApi";
import { message } from 'ant-design-vue';

export default {
  name: 'MonitorsAlertLogAlerts',
  components: {
  },
  async mounted() {
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '告警名称',
          dataIndex: 'objsName'
        },
        {
          title: '所属业务',
          dataIndex: 'ownerBiz',
          customRender: function (row) {
            if (row.text == 'herd-hr-rr') {
              return "牛群-呼吸心跳数据源"
            } else if (row.text == 'herd-rumen-temp') {
              return "牛群-瘤胃温度数据源";
            } else if (row.text == 'device-sprayctocabinet') {
              return "设备-喷淋控制柜数据源";
            } else if (row.text == 'device-envcontrol') {
              return "设备-环控设备数据源";
            } else if (row.text == 'fence-hr-rr') {
              return "牛栏-呼吸心跳数据源";
            } else {
              return "——";
            }
          }
        },
        {
          title: '触发规则数',
          dataIndex: 'alertsNum'
        },
        {
          title: '预警对象类型',
          dataIndex: 'thirdType',
          customRender: function (row) {
            if (row.text == 'cow') {
              return "牛"
            } else if (row.text == 'device') {
              return "设备"
            } else if (row.text == 'cowfence') {
              return "牛栏";
            } else {
              return "—";
            }
          }
        },
        {
          title: '预警对象ID',
          dataIndex: 'thirdId'
        },
        {
          title: '报警等级',
          dataIndex: 'alertLevel',
          customRender: function (row) {
            if (row.text == 1) {
              return "严重"
            } else if (row.text == 2) {
              return "警告"
            }
          }
        },
        {
          title: '数据来源设备ID',
          dataIndex: 'recordId',
          customRender: function (row) {
            if (row.text > 0) {
              return row.text;
            } else {
              return '暂无';
            }
          }
        },
        {
          title: '触发时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      subColumns: [
        {
          title: '告警详情',
          dataIndex: 'alertsText'
        },
        {
          title: '最近异常值',
          dataIndex: 'alertsValue'
        },
        {
          title: '持续几次',
          dataIndex: 'alertsTime'
        },
        {
          title: '最后触发的时间',
          dataIndex: 'lastAlertAt'
        },
        {
          title: '触发开始时间',
          dataIndex: 'alertStartTime'
        },
        {
          title: '触发结束时间',
          dataIndex: 'alertEndTime'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2023/05/28 11:35
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2023/05/28 11:35
     */
    reset() {
      this.where.searchBeginTime = '';
      this.where.searchEndTime = '';
      this.where.ownerBiz = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2023/05/28 11:35
     */
    async remove(row) {
      const result = await MonitorObjectsAlertsLogApi.delete({ objsAlertslogId: row.objsAlertslogId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2023/05/28 11:35
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return MonitorObjectsAlertsLogApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
